<?php
 
    define('QR_IMAGE', true);
    class QRimage extends Lxbase{
    	
        //----------------------------------------------------------------------
        public static function png($frame,$filename = false,$pixelPerPoint = 4,$outerFrame = 4,$saveandprint=FALSE) 
        {
			$webconn = new Lxbase();
            $image = self::image($frame, $pixelPerPoint, $outerFrame);
			$rnum=rand(1000,9999);
			$picurl = dirname(__FILE__)."/../../temp/static/temprcode".$rnum.".png";
			ImagePng($image,$picurl);
			//$pic=$webconn->upload($picurl,"rcode");
            ImageDestroy($image);
			return '/temp/static/temprcode'.$rnum.'.png';
        }
    
        //----------------------------------------------------------------------
        public static function jpg($frame, $filename = false, $pixelPerPoint = 8, $outerFrame = 4, $q = 85) 
        {
			$webconn = new Lxbase();
            $image = self::image($frame, $pixelPerPoint, $outerFrame);
			$rnum=rand(1000,9999);
			$picurl = dirname(__FILE__)."/../../temp/static/temprcode".$rnum.".jpg";
			ImageJpeg($image,$picurl,$q);
			//$pic=$webconn->upload($picurl,"rcode");
            ImageDestroy($image);
			return '/temp/static/temprcode'.$rnum.'.jpg';
        }
    
        //----------------------------------------------------------------------
        private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) 
        {
            $h = count($frame);
            $w = strlen($frame[0]);
            
            $imgW = $w + 2*$outerFrame;
            $imgH = $h + 2*$outerFrame;
            
            $base_image =ImageCreate($imgW, $imgH);
            
            $col[0] = ImageColorAllocate($base_image,255,255,255);
            $col[1] = ImageColorAllocate($base_image,0,0,0);

            imagefill($base_image, 0, 0, $col[0]);

            for($y=0; $y<$h; $y++) {
                for($x=0; $x<$w; $x++) {
                    if ($frame[$y][$x] == '1') {
                        ImageSetPixel($base_image,$x+$outerFrame,$y+$outerFrame,$col[1]); 
                    }
                }
            }
            
            $target_image =ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint);
            ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH);
            ImageDestroy($base_image);
            
            return $target_image;
        }
    }